{% extends 'base.html' %}
{% block title %}
    <title>编辑地址</title>
{% endblock %}
{% block container %}
    <!--主体-->
    <header class="wy-header">
      <div class="wy-header-icon-back"><span></span></div>
      <div class="wy-header-title">编辑地址</div>
    </header>
    <form class="weui-content" method="post" action=".">
      <div class="weui-cells weui-cells_form wy-address-edit">
        <div class="weui-cell">
          <div class="weui-cell__hd">
            <label class="weui-label wy-lab">收货人</label>
          </div>
          <div class="weui-cell__bd">
            <input
              class="weui-input"
              name="username"
              type="text"
              value="{{ form.username.value|default_if_none:'' }}"
              placeholder="请输入收件人"
            />
          </div>
        </div>
          {{ form.username.errors }}
        <div class="weui-cell">
          <div class="weui-cell__hd">
            <label class="weui-label wy-lab">手机号</label>
          </div>
          <div class="weui-cell__bd">
            <input
              class="weui-input"
              type="text"
              name="phone"
              value="{{ form.phone.value|default_if_none:'' }}"
              pattern="[0-9]*"
              placeholder="请输入手机电话"
            />
          </div>
        </div>
          {{ form.phone.errors }}
        <div class="weui-cell">
          <div class="weui-cell__hd">
            <label for="name" class="weui-label wy-lab">所在地区</label>
          </div>
          <div class="weui-cell__bd">
            <input
              class="weui-input"
              id="address"
              name="region"
              type="text"
              value="{{ form.region.value|default_if_none:'' }}"
              readonly=""
            />
          </div>
        </div>
          {{ form.region.errors }}
        <div class="weui-cell">
          <div class="weui-cell__hd">
            <label class="weui-label wy-lab">详细地址</label>
          </div>
          <div class="weui-cell__bd">
            <textarea
              class="weui-textarea"
              name="address"
              placeholder="请输入详细地址"
            >{{ form.address.value|default_if_none:'' }}</textarea>
          </div>
        </div>
          {{ form.address.errors }}
          {{ form.non_field_errors }}
        <div class="weui-cell weui-cell_switch">
          <div class="weui-cell__bd">设为默认地址</div>
          <div class="weui-cell__ft">
              {{ form.is_default }}
          </div>
        </div>
      </div>
      <div class="weui-btn-area">
          <input class="weui-btn weui-btn_primary" type="submit" value="保存此地址">
        <a href="javascript:;" id="id_delete_addr" class="weui-btn weui-btn_warn">删除此地址</a>
      </div>
    </form>
{% endblock %}
{% block footer %}
<script src="/static/js/city-picker.js"></script>
<script>
  $('#address').cityPicker({
    title: '选择出发地',
    onChange: function(picker, values, displayValues) {
      console.log(values, displayValues);
    }
  });
  $("#id_delete_addr").click(function(){
      {#弹窗确认#}
      if (window.confirm('确认删除该地址吗?')){
          $.post("{% url 'accounts:address_delete' form.instance.id %}", function (rest) {
                if(rest === 'ok'){
                    window.location.href = "{% url 'accounts:address_list' %}";
                }
          })
      }
  })
</script>
{% endblock %}
